<template>

  <div>
    <h1>
      安全设置
    </h1>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="*原密码" prop="pass">
        <el-input type="password" v-model.number="ruleForm.pass" autocomplete="off" class="pass_input"></el-input>
      </el-form-item>
      <el-form-item label="*设置新密码" prop="newPass">
        <el-input type="password" v-model.number="ruleForm.newPass" autocomplete="off" class="pass_input"></el-input>
      </el-form-item>
      <el-form-item label="*确认新密码" prop="checkPass">
        <el-input type="password" v-model.number="ruleForm.checkPass" autocomplete="off" class="pass_input"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')" id="btn_mod">确认修改</el-button>

      </el-form-item>
    </el-form>

  </div>
</template>

<script>
  export default {
    data() {
      var checkAge = (rule, value, callback) => {
        let arr1 = [];
        arr1 = JSON.parse(sessionStorage.getItem('user'));
        // console.log(arr1.passWord);

        if (value === '') {
          callback(new Error('请输入原密码'));

        } else if (value !== parseInt(arr1.passWord)) {
          console.log(value)
          console.log(arr1.passWord)
          // if (this.ruleForm.checkPass !== '') {
          //   this.$refs.ruleForm.validateField('checkPass');
          // }
          callback(new Error('原密码输入不正确!'));
        } else {
          callback();
        }
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入新密码'));
        } else if (value === this.ruleForm.pass) {
          callback(new Error('请输入原密码不一样的新密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
            console.log(this.ruleForm.newPass)
            console.log(this.ruleForm.checkPass)
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入新密码'));
        } else if (value !== this.ruleForm.newPass) {
          console.log(value)
          console.log(this.ruleForm.newPass)
          console.log(this.ruleForm.checkPass)
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
        // setTimeout(() => {
        //   if (!Number.isInteger(value)) {
        //     callback(new Error('请输入原密码'));
        //   } else {
        //     if (value < 18) {
        //       callback(new Error('两次输入密码不一致!'));
        //     } else {
        //       callback();
        //     }
        //   }
        // }, 1000);
      };
      return {
        ruleForm: {
          pass: '',
          newPass: '',
          checkPass: '',
          // age: ''
        },
        rules: {
          pass: [{
            validator: checkAge,
            trigger: 'blur'
          }],
          newPass: [{
            validator: validatePass,
            trigger: 'blur'
          }],
          checkPass: [{
            validator: validatePass2,
            trigger: 'blur'
          }]
        }
      };
    },
    methods: {
      submitForm(formName) {
        let arr1 = [];
        arr1 = JSON.parse(sessionStorage.getItem('user'));
        console.log(this.ruleForm.checkPass)
        console.log(arr1.userId)
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$axios({
              url: '/user/changePwd',
              method: 'post',
              data: {
                password: this.ruleForm.checkPass,
                userid: arr1.userId
              }
            }).then((res) => {
              console.log(res);
              // sessionStorage.setItem('user',JSON.stringify(this.tableData));
              alert('修改成功!');
            })
            // alert('修改成功!');
          } else {
            console.log(valid);
            console.log('修改失败!!');
            return false;
          }
        });
      },
    }
  }
</script>
<style lang="less" scoped>
  h1 {
    margin-left: 50px;
    font-size: 26px;
  }

  .pass_input {
    width: 30%;
  }
  .el-form-item{
    margin: 25px 0;
  }
  #btn_mod{
    margin-left: 10%;
  }
</style>